﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace Passwords.Controls
{
    /// <summary>
    /// Extends a ListBox to bind the selected items with a ViewModel.
    /// </summary>
    public class MultiSelectBehavior : Behavior<ListBox>
    {

        public IList SelectedItems
        {
            get { return (IList)GetValue(SelectedItemsProperty); }
            set { SetValue(SelectedItemsProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MultiSelectBehavior), new PropertyMetadata(null));

        protected override void OnAttached()
        {
            ListBox box = this.AssociatedObject;
            box.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            AssociatedObject.SelectionChanged -= OnSelectionChanged;
            base.OnDetaching();
        }


        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedItems = AssociatedObject.SelectedItems;
        }

    }
}
